package com.hsnn.medstgmini.settlement.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.settlement.model.SetPayment;
import com.hsnn.medstgmini.settlement.model.form.Payment;
import com.hsnn.medstgmini.settlement.model.form.SetSettlementForm;
import com.hsnn.medstgmini.settlement.service.PaymentManager;
import com.hsnn.medstgmini.settlement.service.SettlementFirstManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.DbUtil;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseNumber;

@Controller
@RequestMapping("/nextCheckPayment")
public class RecheckPaymentController {
	private static final Logger log = Logger.getLogger(PaymentController.class);

	@Autowired
	private PaymentManager paymentManager;
	
	@Autowired
	private SettlementFirstManager settlementFirstManager;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private HttpSession session;


	/**
	 * @category 获取当前登陆人信息
	 * @date 2015年6月14日
	 * @return
	 */
	public SysUser getUser() {
		return (SysUser) session.getAttribute(Constants.USERINFO);
	}
	
	/**
	 * @category 跳转到中心端复审的支付单页面
	 * @author 韩守松
	 * @date   2015年7月27日
	 * @param  @return
	 */
	@RequestMapping("/toRecheckPaymentList")
	public String toRecheckSetPaymentList(){
		return "/settle/payment/recheckSetPaymentList";
	}
	
	@RequestMapping("/getRecheckSetPaymentData")
	@ResponseBody
	public Pagination getRecheckSetPaymentData(Payment payment){
		log.info("enter getPaymentList method");
		Pagination page = new Pagination(request);
		try {
			DbUtil.jsonToMap(request, page);
			paymentManager.getPaymentList(payment, page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
	
	/**
	 * @category 跳转到中心复审的详情列表页面
	 * @author 韩守松
	 * @date   2015年7月28日
	 * @param  @return
	 */
	@RequestMapping("/toRecheckCenterSetPaymentDetailList")
	public String toRecheckCenterSetPaymentDetailList(Model model,String paymentBillId){
		SetPayment setPayment = paymentManager.getPaymentById(paymentBillId);
//		setPayment.setTotalAmountStr(ParseNumber.toStrDecimal(setPayment.getTotalAmount()));
//		setPayment.setCreateDatetimeStr(DateUtil.dateTime(setPayment.getCreateDatetime()));
		model.addAttribute("setPayment", setPayment);
		return "/settle/payment/recheckCenterSetPaymentDetailList";
	}
	
	@RequestMapping("/getRecheckCenterSetPaymentDetailListData")
	@ResponseBody
	public Pagination getRecheckCenterSetPaymentDetailListData(Payment payment){
		Pagination page = new Pagination(request);
		try {
			paymentManager.getPaymentDetailList(payment, page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 跳转到支付明细列表页面
	 * @author 韩守松
	 * @date   2015年7月28日
	 * @param  @return
	 */
	@RequestMapping("/toRecheckSetPaymentDetailList")
	public String toRecheckSetPaymentDetailList(Model model,String settlementId,String paymentBillId){
		SetSettlementForm SetSettlementForm = settlementFirstManager.getSettlementInfoById(settlementId);
		model.addAttribute("obj", SetSettlementForm);
		model.addAttribute("paymentBillId", paymentBillId);
		model.addAttribute("settlementId2",request.getParameter("settlementId2"));
		return "/settle/payment/recheckSetPaymentDetailList";
	}
	
	@RequestMapping("/getRecheckSetPaymentDetailListData")
	@ResponseBody
	public Pagination getRecheckSetPaymentDetailListData(Payment payment){
		Pagination page = new Pagination(request);
		try {
			paymentManager.getSettlementDetailList(payment, page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 中心审核支付单
	 * @author 韩守松
	 * @date   2015年7月29日
	 * @param  @return
	 */
	@RequestMapping("/checkPaymentBill")
	@ResponseBody
	public boolean checkPaymentBill(SetPayment setPayment,SysUser user){
		boolean flag = false;
		try {
			user = getUser();
			flag = paymentManager.updatePaymentStatusById(setPayment, user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	//////////////////////////////////////////////////////////////////////////////
	/**
	* @category 跳转支付查看，支付单支付明细
	* @date 2015年7月27日
	* @return
	*/
	@RequestMapping("/toCenterRecheckSetmentDetailList")
	public String toCenterRecheckSetmentDetailList(Model model, Payment payment) {
		log.info("enter toCenterRecheckSetmentDetailList method!");
		model.addAttribute("payment", payment);
		String settlementId = payment.getSettlementId();
		SetSettlementForm settlement = settlementFirstManager
		.getSettlementInfoById(settlementId);
		model.addAttribute("info", settlement);
		return "/settle/centerRecheckSetmentDetailList";
	}
	
	@RequestMapping("/getCenterRecheckSetmentDetailList")
	@ResponseBody
	public Pagination getCenterRecheckSetmentDetailList(String settlementId){
		Pagination page = new Pagination(request);
		page.getConditions().put("settlementId", settlementId);
		page.getConditions().put("hospName", request.getParameter("hospName"));
		try {
			paymentManager.getSetmenDetailList(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
}
